package org.techmore.hakka;

import org.techmore.bussiness.QuestionsBeanDataHandler;
import org.techmore.common.utils.activity.ActivityUtils;
import org.techmore.common.utils.sound.SoundPoolUtils;
import org.techmore.valueobject.QuestionVO;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class AnswerQuestionActivity extends Activity {
	
	private View answer_question_view = null;
	private TextView answer_question_question = null;
	private ImageView answer_question_image = null;
	private TextView answer_question_A = null;
	private TextView answer_question_B = null;
	private TextView answer_question_C = null;
	
	private QuestionsBeanDataHandler questionsBeanDataHandler = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ActivityUtils.getInstance().setScreenOrientation(this);
		questionsBeanDataHandler = QuestionsBeanDataHandler.getInstance();
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
		answer_question_view = LayoutInflater.from(this).inflate(R.layout.answer_question, null);
		answer_question_question = (TextView) answer_question_view.findViewById(R.id.answer_question_question);
		answer_question_image = (ImageView) answer_question_view.findViewById(R.id.answer_question_image);
		answer_question_A = (TextView) answer_question_view.findViewById(R.id.answer_question_A);
		answer_question_B = (TextView) answer_question_view.findViewById(R.id.answer_question_B);
		answer_question_C = (TextView) answer_question_view.findViewById(R.id.answer_question_C);
		
		int index = questionsBeanDataHandler.getQuestionIndex();
		QuestionVO questionVO = questionsBeanDataHandler.getQuestionVO(index);
		
		answer_question_question.setText("  "+questionVO.getQuestionContent());
		
		answer_question_A.setText("1."+questionVO.getAnswerA());
		answer_question_A.setTag(questionVO.getAnswerA());
		answer_question_B.setText("2."+questionVO.getAnswerB());
		answer_question_B.setTag(questionVO.getAnswerB());
		answer_question_C.setText("3."+questionVO.getAnswerC());
		answer_question_C.setTag(questionVO.getAnswerC());
		
		
		setContentView(answer_question_view);
		
		answer_question_A.setOnClickListener(new QuestionListenner(questionVO.getTrueAnswer(),questionVO.getAnswerDescription()));
		answer_question_B.setOnClickListener(new QuestionListenner(questionVO.getTrueAnswer(),questionVO.getAnswerDescription()));
		answer_question_C.setOnClickListener(new QuestionListenner(questionVO.getTrueAnswer(),questionVO.getAnswerDescription()));
	}
	
	
	private class QuestionListenner implements View.OnClickListener{
		private String trueAnswer;
		private String description;
		public QuestionListenner(String trueAnswer, String description) {
			this.trueAnswer = trueAnswer;
			this.description = description;
		}
		@Override
		public void onClick(View v) {
			SoundPoolUtils.getInstance(AnswerQuestionActivity.this).playSound(SoundPoolUtils.SOUND_BUTTON_CLICK);
			String value = v.getTag().toString();
			Intent intent = new Intent();
			intent.putExtra("value", value);
			intent.putExtra("trueAnswer", trueAnswer);
			intent.putExtra("description", description);
			if(value.equals(trueAnswer)){
				QuestionsBeanDataHandler.getInstance().increaseTotalScore();
				intent.putExtra("results", true);
			}else{
				intent.putExtra("results", false);
			}
			intent.setClass(AnswerQuestionActivity.this, AnswerResultActivity.class);
			AnswerQuestionActivity.this.startActivity(intent);
			AnswerQuestionActivity.this.overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
			AnswerQuestionActivity.this.finish();
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
//		return super.onKeyDown(keyCode, event);
		return false;
	}
	
	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

}
